import numpy as np
from sklearn import svm
from knn import *
from data_format import x_train,y_train,x_test,y_test
from sklearn.neighbors import KNeighborsClassifier


# 特征缩放
x_train = (x_train - np.min(x_train, axis=0)) / (np.max(x_train, axis=0) - np.min(x_train, axis=0))
x_test = (x_test - np.min(x_test, axis=0)) / (np.max(x_test, axis=0) - np.min(x_test, axis=0))
k_num=[]
scores=[]

# knn classifier
for k in range(1,100,2):
    print('k:{}'.format(k))
    k_num.append(k)

    #print('train accuracy: {:.3}'.format(clf.score()))
    estimator = KNeighborsClassifier(n_neighbors=k)
    estimator.fit(x_train, y_train)

    # 模型评估
    # 方法1：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("直接比对真实值和预测值:\n", y_test == y_predict)
    # 方法2：计算准确率
    score=estimator.score(x_test,y_test)

   # y_test_pred = clf.predict(x_train)
    print('test accuracy: {:.3}'.format(score))
    scores.append(score)

def draw_roc():
    random_state = np.random.RandomState(0)
    svm_new = svm.SVC(kernel='linear', probability=True, random_state=random_state)

    ###通过decision_function()计算得到的y_score的值，用在roc_curve()函数中
    y_score = svm_new.fit(x_train, y_train).decision_function(x_test)

    # Compute ROC curve and ROC area for each class
    fpr, tpr, threshold = roc_curve(y_test, y_score)  ###计算真阳性率和假阳性率
    roc_auc = auc(fpr, tpr)  ###计算auc的值

    plt.figure()
    lw = 2
    plt.figure(figsize=(10, 10))
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)  ###假正率为横坐标，真正率为纵坐标做曲线
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()

draw_roc()
print(y_train.shape)
print(y_predict.shape)
#print(classification_report(y_train, y_predict))
plt.plot(k_num,scores)
plt.show()






